<?php
namespace app\common\model;
use think\Model;
use think\Db;
class CoachLevel extends Model
{
    //定义表名
    protected $name = 'massage_service_coach_level';
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){
        $data['create_time'] = time();
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataInfo($dis){
        $res = $this->where($dis)->find();
        return !empty($res)?$res->toArray():[];
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){
        $res = $this->where($dis)->update($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){
        $data = $this->where($dis)->order('time_long desc,id desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInit($uniacid){
        //查询有无记录
        $info = $this->where(['uniacid'=>$uniacid])->order('date_str desc')->find();
        //没有就用订单的第一天
        if(empty($info)){
            $order_model = new Order();
            $start = $order_model->where(['uniacid'=>$uniacid])->min('create_time');
            $start = !empty($start)?$start-86400:0;
        }else{
            $start = $info['date_str'];
        }
        //没有记录就用今天
        $start = !empty($start)?$start:time();
        //确保零点
        $start = strtotime(date('Y-m-d',$start));
        $eve   = strtotime(date('Y-m-d',time())) - $start;
        $eve   = $eve/86400;
        if(!empty($eve)){
            for ($i=1;$i<=$eve;$i++){
                $insert = [
                    'uniacid' => $uniacid,
                    'date'    => date('Y-m-d',$start+$i*86400),
                    'date_str'=> $start+$i*86400
                ];
                $this->dataAdd($insert);
            }
        }
        return true;
    }
}